package com.mmall.user.provider.controller;

import com.common.framework.bo.TokenBO;
import com.mmall.common.spring.security.LoginRequired;
import com.mmall.common.spring.security.UserSecurityContextHolder;
import com.mmall.user.api.bo.UserBO;
import com.mmall.user.api.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @author jianghucoding
 * @date 2019/10/17
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    private static final Logger LOGGER = LogManager.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @GetMapping("/info")
    @LoginRequired
    public UserBO getUser() {
        Long userId = UserSecurityContextHolder.getContext().getUserId();
        return userService.getUser(userId);
    }

    @GetMapping("/sendAuthCode")
    public String sendAuthCode(@NotNull Long mobile) {
        return userService.sendAuthCode(mobile);
    }

    @GetMapping("/register")
    public TokenBO register(@NotNull Long mobile, @NotEmpty String authCode) {
        return userService.createUser(mobile, authCode);
    }

    /**
     * 通过手机号发送验证码的方式登录
     * @param mobile
     * @param authCode
     * @return
     */
    @GetMapping("/login")
    public TokenBO login(@NotNull Long mobile, @NotEmpty String authCode) {
        return userService.login(mobile, authCode);
    }

    @GetMapping("/testLog")
    public void test() {
        LOGGER.info("----------------------info");
        LOGGER.debug("----------------------debug");
        LOGGER.error("----------------------error");
        LOGGER.warn("----------------------warn");
    }
}
